package com.loop.unclelend.ui.presenter

import com.loop.unclelend.app_config.UncleConfigs
import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Launch
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.helper.SharedHelper
import com.loop.unclelend.utils.helper.SystemAnalysisHelper
import com.loop.unclelend.utils.helper.TestDataLibInit
import com.loop.unclelend.utils.permission.Permission
import com.loop.unclelend.utils.permission.XXPermissions
import org.json.JSONObject
import java.util.TimeZone

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/3 16:00
 * @Annotation：启动页
 */
class Pre_Launch(private val iView: IView_Launch): ProPresenter(iView) {

    /**
     * 获取地区转发
     */
    fun requestTimeZoneAction() {
        iView.showUncleLoading()
        val params = JSONObject()
        params.put("p", UncleConfigs.UNCLE_APPNAME)
        params.put("tz", TimeZone.getDefault().id)
        val httpUrl = ApiService.UNCLE_BASE_URL + ApiService.API_URL.UNCLE_API_TIMEZONE_URL
        doPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                val dataString = data.optString("data")
                val timeZone = dataString.split(",")[0]
                val zoneCode = dataString.split(",")[1]

                //保存转发地区
                SharedHelper.saveUncleSPData(SharedHelper.UNCLE_SPKEY.UNCLE_TIMEZONE,timeZone)
                //保存转发地区 区号
                SharedHelper.saveUncleSPData(SharedHelper.UNCLE_SPKEY.UNCLE_TIMEZONE_CODE,zoneCode)
                //初始化analysis lib 赋值 转发地区
                SystemAnalysisHelper.initAnalysisLibAtZoneSuccess()
                //初始化 data lib
                TestDataLibInit.initDataLibAtZoneSuccess()
                //添加埋点
                UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LAUNCH_START)

                //坦桑需要选择语言
                val isChooseLanguage = SharedHelper.getUncleSPBoolean(SharedHelper.UNCLE_SPKEY.UNCLE_TANZANIA_LANGUAGE)
                if (UncleConfigs.isTanz() && !isChooseLanguage){
                    iView.hideUncleLoading()
                    iView.showUncleTanzaniaLanguageDialog()
                    return
                }
                requestCommonConfigAction()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取APP配置信息
     */
    fun requestCommonConfigAction() {
        val params = JSONObject().apply {
            put("product",UncleConfigs.UNCLE_APPNAME)
            put("path",ApiService.API_PATH.UNCLE_API_APP_INFO_PATH)
        }
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        doPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideUncleLoading()
                val obj = data.optJSONObject("obj")
                //保存所有数据
                if (obj != null) {
                    SharedHelper.saveUncleSPData(SharedHelper.UNCLE_SPKEY.UNCLE_COMMON,obj.toString())
                }

                // TODO: PhoneState为必须申请的权限
                if (!XXPermissions.isGranted(iView.getAct(), Permission.READ_PHONE_STATE)){
                    //进入权限页
                    iView.showUnclePermissionPage()
                } else {
                    //已登录 - 进入主页
                    if (SharedHelper.getUncleSPBoolean(SharedHelper.UNCLE_SPKEY.UNCLE_LOGIN)){
                        //初始化analysis lib 赋值 用户id 和 token
                        SystemAnalysisHelper.initAnalysisLibAtLoginSuccess()
                        iView.showUncleMainPage()
                    } else {//进入登录页
                        iView.showUncleLoginPage()
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }
}